

import ViewBase from "../../engine/ViewBase";
import { SocketClient } from "../../net/SocketClient";
import { UserModel } from "../model/UserModel";
import { UIHelper } from "../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewExchangeMeili extends ViewBase {

    private txtGiveNum: cc.EditBox
    private lbcoins: cc.Label
    private maxNum: number = 0

    onEnable(): void {
        super.onEnable()
        this.txtGiveNum = this.getChild(this.node, "txtGiveNum", cc.EditBox)
        this.lbcoins = this.getChild(this.node, "lbcoins", cc.Label)
        let btnExchange: cc.Node = this.findChild(this.node, "btnExchange")
        UIHelper.onAddEvent(btnExchange, () => {
            if (this.txtGiveNum.string == "") {
                UIHelper.showTip("请输入兑换魅力值数量")
                return;
            }
            let num: number = parseInt(this.txtGiveNum.string)
            if(num <= this.maxNum){
                this.closeView()
                SocketClient.req_exchange_meili(num)
            }else{
                UIHelper.showTip("超过最大兑换量")
            }
        })
        this.refresh()
    }

    private refresh() {
        this.maxNum = Math.floor(UserModel.instance.coins / 100000)
        this.lbcoins.string = "最高可兑换" + this.maxNum
    }

}
